﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using childApp.common;

namespace childApp.member
{
    public partial class DataGridForm : Form
    {
        public DataGridForm()
        {
            InitializeComponent();
        }


        private DataSet getMems(string name, string sex)
        {
            DataSet ds = null;
            StringBuilder sb = new StringBuilder("select * from member where 1=1");

            if(name != null && name.Trim() != "")
            {
                sb.Append(" and name like '%"+name.Trim()+"%'");
            }
            if(sex != null && sex.Trim() != "")
            {
                sb.Append(" and sex = '" + sex.Trim()+ "'");
            }

            ds = DbCommon.GetDataSet(sb.ToString(), "member");

            return ds;
        }

        private void DataGridForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = getMems(null,null);
            this.dataGridView1.DataMember = "member";

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text;
            string sex = this.cmbSex.Text;

            this.dataGridView1.DataSource = getMems(name, sex);
            this.dataGridView1.DataMember = "member";
            
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (this.dataGridView1.RowCount > 0)
            {
                string id = dataGridView1.CurrentRow.Cells["id"].Value.ToString();

                if (id != "")
                {
                    MemModifyForm form = new MemModifyForm();
                    form.lbNo.Text = id;

                    form.txtName.Text  = dataGridView1.CurrentRow.Cells["name"].Value.ToString();
                    form.txtAddr.Text = dataGridView1.CurrentRow.Cells["addr"].Value.ToString();
                    form.txtScore.Text = dataGridView1.CurrentRow.Cells["score"].Value.ToString();
                    form.dtpBirth.Text = dataGridView1.CurrentRow.Cells["birth"].Value.ToString();

                    //string sex = dataGridView1.CurrentRow.Cells["sex"].Value.ToString();
                    //int index = form.cmbSex.FindString(sex);
                    //form.cmbSex.SelectedIndex = index;
                    form.cmbSex.Text = dataGridView1.CurrentRow.Cells["sex"].Value.ToString();

                    form.ShowDialog();
                    form.Dispose();


                    this.dataGridView1.DataSource = getMems(null, null);
                    this.dataGridView1.DataMember = "member";

                }
            }
        }




    }
}
